#!/usr/bin/env bash

include "libs/arguments.sh"
include "libs/test.sh"

function message_box()
{
    local ret=$?
    if [ "$1" != "" ]; then
        ret="$1"
    fi

    if [ "$OHOS_WINDOW_USE" == "true" ]; then
        if [ $ret = 0 ]; then
            zenity --info --text="$CONTAINER_NAME $COMMAND success" 2>/dev/null &
        else
            zenity --error --text="$CONTAINER_NAME $COMMAND failure" 2>/dev/null &
        fi
    fi
    return $ret
}

function zenity:list()
{
    arguments $*
    local column_size=0
    local keys=()
    local others=()
    local zenity_args=()
    while arguments:have_next; do
        if ! arguments:is_option; then
            echo "$(arguments:get_now) useless"
            arguments:next
            continue
        fi

        local key val
        if arguments:is_kv_option; then
            key="$(arguments:parse_key_of_kv_option)"
            val="$(arguments:parse_val_of_kv_option)"
            arguments:next
        else
            key="$(arguments:parse_option)"
            arguments:next
            val="$(arguments:get_now)"
            arguments:next
        fi

        case "$key" in
            width) zenity_args+=("--$key=$val") ;;
            height) zenity_args+=("--$key=$val") ;;
            text) zenity_args+=("--$key=$val") ;;
            title) zenity_args+=("--$key=$val") ;;
            columns)
                for column in ${val//:/ }; do
                    zenity_args+=("--column=$column")
                    ((column_size++))
                done
                ;;
            keys)
                keys=(${val//:/ })
                ;;
            others)
                others=(${val//:/ })
                ;;
            *)
                echo "zenity:list: cannot parse option"
                exit 1
                ;;
        esac
    done

    for (( i = 0; i < ${#keys[*]}; i++ )); do
        zenity_args+=(${!keys[$i]})
        zenity_args+=(${keys[$i]})
        for (( j = 0; j < $column_size - 2; j++ )); do
            zenity_args+=(${others[$((i * (column_size - 2) + j))]})
        done
    done

    local result="$(zenity --list --separator=" " --checklist ${zenity_args[*]})$((retval=$?))"
    if [ $retval == 0 ]; then
        for key in ${keys[*]}; do
            eval "$key=false"
        done
        for key in ${result::-${#retval}}; do
            eval "$key=true"
        done
    else
        return 1
    fi
}

function zenity:list:test1()
{
    function xdotools()
    {
        while true; do
            windowid=$(xdotool search --name "zenity_test[: a-zA-Z_-]*$" 2>/dev/null)
            [ "$windowid" != "" ] && break
        done
        xdotool mousemove --window $windowid --sync 50 80 click 1
        xdotool mousemove --window $windowid --sync 440 175 click 1
    }
    (xdotools &)
    use_docker=true
    use_window=true
    git_update=true
    test:should_return_true zenity:list \
        --width=500 \
        --height=200 \
        --text="点击docker然后确认" \
        --title="zenity_test:config ohos_tool" \
        --columns="是否启用:特性:描述" \
        --keys="use_docker:use_window:git_update" \
        --others="用docker编译:用窗口提醒耗时操作完成:git仓库自动更新"
    test:var_should_be use_docker false
    test:var_should_be use_window true
    test:var_should_be git_update true
}
test:register_test zenity:list:test1

function zenity:list:test2()
{
    function xdotools()
    {
        while true; do
            windowid=$(xdotool search --name "zenity_test[: a-zA-Z_-]*$" 2>/dev/null)
            [ "$windowid" != "" ] && break
        done
        xdotool mousemove --window $windowid --sync 50 80 click 1
        xdotool mousemove --window $windowid --sync 340 175 click 1
    }
    (xdotools &)
    use_docker=true
    use_window=true
    git_update=true
    test:should_return_false zenity:list \
        --width=500 \
        --height=200 \
        --text="点击docker然后取消" \
        --title="zenity_test:config ohos_tool" \
        --columns="是否启用:特性:描述" \
        --keys="use_docker:use_window:git_update" \
        --others="用docker编译:用窗口提醒耗时操作完成:git仓库自动更新"
    test:var_should_be use_docker true
    test:var_should_be use_window true
    test:var_should_be git_update true
}
test:register_test zenity:list:test2

function zenity:list:test3:manual()
{
    a=true
    b=false
    c=true
    d=false
    zenity:list \
        --width=500 \
        --height=200 \
        --text="五列" \
        --title="5col" \
        --columns="A:B:C:D:E" \
        --keys="a:b:c:d" \
        --others="A1:A2:A3:B1:B2:B3:C1:C2:C3:D1:D2:D3"
}
test:register_test zenity:list:test3:manual

# retval: [0]success [1]failed
# stdout: string(inputed string)
# params: (text, placeholder)
function zenity:input_string()
{
    zenity --entry --text="$1" --entry-text="$2"
}

# retval: [0]success [1]failed
# stdout: string(selected directory)
# params: (title: string)
function zenity:dir_selector()
{
    zenity --file-selection --directory --title="$1"
}

# retval: [0]success [1]failed
# stdout: string(selected entry)
# params: (obj_name: string, ...: obj)
function zenity:list_selector()
{
    local obj_name="$1"
    shift
    zenity --list --text="请选择一个$obj_name" --column="$obj_name" $*
}

# retval: [0]success [1]failed
# stdout: string(selected file)
# params: (title: string, default_select: path)
function zenity:file_selector()
{
    zenity --file-selection --title="$1" --filename="$2"
}
